package com.dust.web.controller.monitor;

import com.dust.common.annotation.Log;
import com.dust.common.core.page.TableDataInfo;
import com.dust.framework.shiro.service.SysPasswordService;
import com.dust.system.domain.SysLoginInfo;
import com.dust.common.core.controller.BaseController;
import com.dust.common.core.domain.AjaxResult;
import com.dust.common.enums.BusinessType;
import com.dust.common.utils.poi.ExcelUtil;
import com.dust.system.service.ISysLoginInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 系统访问记录
 */
@Controller
@RequestMapping("/monitor/login/info")
public class SysLoginInfoController extends BaseController {
    private final String prefix = "monitor/login/info";

    @Autowired
    private ISysLoginInfoService loginInfoService;

    @Autowired
    private SysPasswordService passwordService;

    @RequiresPermissions("monitor:login:info:view")
    @GetMapping()
    public String loginInfo() {
        return prefix + "/info";
    }

    @RequiresPermissions("monitor:login:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysLoginInfo loginInfo) {
        startPage();
        List<SysLoginInfo> list = loginInfoService.selectLoginInfoList(loginInfo);
        return getDataTable(list);
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @RequiresPermissions("monitor:login:info:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysLoginInfo logininfor) {
        List<SysLoginInfo> list = loginInfoService.selectLoginInfoList(logininfor);
        ExcelUtil<SysLoginInfo> util = new ExcelUtil<>(SysLoginInfo.class);
        return util.exportExcel(list, "登录日志");
    }

    @RequiresPermissions("monitor:login:info:remove")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(loginInfoService.deleteLoginInfoByIds(ids));
    }

    @RequiresPermissions("monitor:login:info:remove")
    @Log(title = "登录日志", businessType = BusinessType.CLEAN)
    @PostMapping("/clean")
    @ResponseBody
    public AjaxResult clean() {
        loginInfoService.cleanLoginInfo();
        return success();
    }

    @RequiresPermissions("monitor:login:info:unlock")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @PostMapping("/unlock")
    @ResponseBody
    public AjaxResult unlock(String loginName) {
        passwordService.clearLoginRecordCache(loginName);
        return success();
    }
}
